package com.framework.loippi.controller.patient;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import com.framework.loippi.controller.GenericController;
import com.framework.loippi.entity.TUser;
import com.framework.loippi.service.TUserService;
import com.framework.loippi.support.Page;
import com.framework.loippi.utils.ParameterUtils;
import com.framework.loippi.utils.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.framework.loippi.entity.Patient;
import com.framework.loippi.service.PatientService;
import com.framework.loippi.support.Message;
import com.framework.loippi.support.Pageable;
import com.framework.loippi.utils.ParameterUtils;
import com.framework.loippi.utils.StringUtil;
import java.util.HashMap;

/**
 * Controller - 患者表
 * 
 * @author wmj
 * @version 2.0
 */
@Controller("adminPatientController")
@RequestMapping({ "/admin/patient" })
public class PatientController extends GenericController {

	@Resource
	private PatientService patientService;
	@Resource
	private TUserService userService;
	/**
	 * 跳转添加页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/add" }, method = { RequestMethod.GET })
	public String add(ModelMap model) {
		List<TUser> users = userService.findAll();
		model.addAttribute("users", users);
		return "/admin/patient/add";
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(Patient patient, RedirectAttributes redirectAttributes) {
		patientService.save(patient);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}

	/**
	 * 添加诊断说明
	 * @param imUserId
	 * @param explains
	 * @return
	 */
	/*@PostMapping("/addExplains")
	public boolean addExplains(Long imUserId, String explains){
		Patient patient = patientService.getPatientByImUserId(imUserId);
		patient.setExplains(explains);
		return patientService.updateEntity(patient)>0L;
	}*/

	/**
	 * 编辑
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable Long id, ModelMap model) {
		Patient patient = patientService.find(id);
		patient.setUser(userService.find(patient.getUserId()));
		List<TUser> users = userService.findAll();
		model.addAttribute("users", users);
		model.addAttribute("patient", patient);
		return "/admin/patient/edit";
	}
	
	
	/**
	 * 详情
	 */
	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public String view(@PathVariable Long id, ModelMap model) {
		Patient patient = patientService.find(id);
		model.addAttribute("patient", patient);
		return "/admin/patient/view";
	}
	

	/**
	 * 更新
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(Patient patient, RedirectAttributes redirectAttributes) {
		patientService.update(patient);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}

	/**
	 * 列表查询
	 * 
	 * @param pageable
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/list" }, method = { RequestMethod.GET })
	public String list(Pageable pageable,HttpServletRequest request, ModelMap model) {
		
		Map<String, Object> paramter = ParameterUtils.getParametersMapStartingWith(request, "filter_");
		Map map=new HashMap();
		  for (String key : paramter.keySet()) {  
	            if(!StringUtil.isEmpty( paramter.get(key).toString())){
	            	map.put(key,  paramter.get(key));
	            }
	        }
		String keyword = request.getParameter("keyword");
		if (!StringUtils.isNullOrEmpty(keyword)) {
			map.put("nameLike", keyword);
			map.put("phoneLike", keyword);
			map.put("addressLike", keyword);
			map.put("nickNameLike", keyword);
		}
		map.put("order","create_time desc");
		pageable.setParameter(map);
		Page<Patient> page = this.patientService.findByPage(pageable);
		model.addAttribute("paramter", paramter);
		model.addAttribute("page", page);
		return "/admin/patient/list";
	}

	/**
	 * 删除操作
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = { "/delete" }, method = { RequestMethod.POST })
	public @ResponseBody Message delete(Long[] ids) {
		this.patientService.deleteAll(ids);
		return SUCCESS_MESSAGE;
	}

}
